//
//  HUDManager.m
//  yilingdoctorCRM
//
//  Created by zhangxi on 14/10/28.
//  Copyright (c) 2014年 yuntai. All rights reserved.
//

#import "HUDManager.h"

static MBProgressHUD *HUDView;
@implementation HUDManager

+ (void)showWarningWithText:(NSString *)text {
    [HUDManager hideHUDView];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;

//    UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.labelText = text;
    hud.mode = MBProgressHUDModeText;
    hud.dimBackground = NO;
    hud.margin = 12.f;
    [hud hide:YES afterDelay:1.0];
}

+ (void)showLoadingHUDView:(UIView*)view
{
    [HUDManager hideHUDView];
    HUDView = [MBProgressHUD showHUDAddedTo:view animated:YES];
    HUDView.mode = MBProgressHUDModeIndeterminate;
    HUDView.margin = 18.f;
    HUDView.opacity = 0.55;
    HUDView.dimBackground = NO;
    HUDView.labelText = @"努力加载中";
    HUDView.minShowTime = 0.3;
    HUDView.graceTime = 10;
    HUDView.labelFont = [UIFont boldSystemFontOfSize:14];
}

+ (void)showLoadingHUDView:(UIView*)view withText:(NSString *)text {
    [HUDManager hideHUDView];
    HUDView = [MBProgressHUD showHUDAddedTo:view animated:YES];
    HUDView.mode = MBProgressHUDModeIndeterminate;
    HUDView.margin = 18.f;
    HUDView.opacity = 0.55;
    HUDView.dimBackground = YES;
    HUDView.labelText = text;
    HUDView.minShowTime = 0.3;
    HUDView.labelFont = [UIFont boldSystemFontOfSize:14];
}

+ (void)hideHUDView
{
    [HUDView hide:YES];
}
+(void)showUploadHubViewWithExecuting:(void(^)(MBProgressHUD *hub))executing{
    [HUDManager hideHUDView];
    HUDView = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
    HUDView.mode = MBProgressHUDModeDeterminate;
    HUDView.labelText = @"上传中...";
    HUDView.detailsLabelText = @"请等候";
    HUDView.dimBackground = NO;
    HUDView.animationType = MBProgressHUDAnimationZoomIn;
    
    [HUDView showAnimated:YES whileExecutingBlock:^{
//        weakself.progess = 0.f;
//        while (weakself.progess<1.0f) {
//            usleep(50000);
//            weakself.progess += 0.1;
//            weakself.hub.progress = self.progess;
//        }
        executing(HUDView);
    } completionBlock:^{
            usleep(450000);

//        [HUDView removeFromSuperview];
//        HUDView = nil;
    }];
    
}
@end
